/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean_Admin_Product;

import beanQuestion.QuestionBean;
import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;

/**
 *
 * @author KaiLee
 */
public class connectSQLQuestion {

    private Connection conn;

    public connectSQLQuestion() {
        conn = getConnection("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/banhang?useUnicode=true&characterEncoding=UTF-8", "root", "");
    }

    public Connection getConnection(String dbClass, String dbUrl, String user, String pass) {
        Connection con = null;
        try {
            Class.forName(dbClass);
            con = DriverManager.getConnection(dbUrl, user, pass);

        } catch (Exception ex) {
        }
        return con;
    }

    public ArrayList<QuestionBean> getQuestion() {
        ArrayList<QuestionBean> list = new ArrayList<QuestionBean>();

        try {
            String query = "select * from question";
            Statement st = (Statement) conn.createStatement();
            ResultSet rs = st.executeQuery(query);
            int i = 0;
            while (rs.next()) {
                list.add(new QuestionBean(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4)));
            }
        } catch (Exception ex) {
        }
        return list;
    }

    public int deleteQuestion(String id) {
        try {
            String query = "delete from question where id = ?";
            PreparedStatement ps = (PreparedStatement) conn.prepareCall(query);
            ps.setString(1, id);
            return ps.executeUpdate();
        } catch (Exception ex) {
            System.out.print("loi");
        }
        return -1;
    }

   
}
